# 自动化Hook测试脚本

Write-Host "`n========================================" -ForegroundColor Green
Write-Host "🎯 自动化Hook测试" -ForegroundColor Green
Write-Host "========================================`n" -ForegroundColor Green

# 1. 检查麦当劳APP是否运行
Write-Host "📱 检查麦当劳APP..." -ForegroundColor Cyan
$process = frida-ps -Uai | Select-String "mcdonalds"
if ($process) {
    Write-Host "✅ 麦当劳APP正在运行" -ForegroundColor Green
    Write-Host $process -ForegroundColor Yellow
} else {
    Write-Host "❌ 麦当劳APP未运行！请先启动APP" -ForegroundColor Red
    exit 1
}

# 2. 启动Frida Hook（使用包名）
Write-Host "`n🔧 启动Frida Hook..." -ForegroundColor Cyan
Write-Host "目标: com.mcdonalds.gma.cn" -ForegroundColor Yellow
Write-Host "脚本: hook_simple_test.js`n" -ForegroundColor Yellow

# 使用Start-Job在后台运行Frida，这样可以继续执行脚本
$fridaJob = Start-Job -ScriptBlock {
    param($scriptPath)
    Set-Location $scriptPath
    frida -U com.mcdonalds.gma.cn -l hook_simple_test.js 2>&1
} -ArgumentList (Get-Location).Path

# 等待Frida初始化
Write-Host "⏳ 等待Frida初始化（3秒）..." -ForegroundColor Yellow
Start-Sleep -Seconds 3

# 检查Job状态
if ($fridaJob.State -eq "Running") {
    Write-Host "✅ Frida已启动`n" -ForegroundColor Green
} else {
    Write-Host "❌ Frida启动失败！" -ForegroundColor Red
    Receive-Job -Job $fridaJob
    exit 1
}

# 3. 调用API触发麦当劳APP
Write-Host "========================================" -ForegroundColor Green
Write-Host "🚀 调用API触发加密过程..." -ForegroundColor Cyan
Write-Host "========================================`n" -ForegroundColor Green

$apiUrl = "http://120.27.155.222:9999/api/message/send-test"
Write-Host "URL: $apiUrl`n" -ForegroundColor Yellow

try {
    $response = Invoke-WebRequest -Uri $apiUrl -TimeoutSec 10
    
    Write-Host "✅ API调用成功！" -ForegroundColor Green
    Write-Host "状态码: $($response.StatusCode)`n" -ForegroundColor Yellow
    
    # 解析响应
    $json = $response.Content | ConvertFrom-Json
    Write-Host "📦 返回数据:" -ForegroundColor Cyan
    Write-Host "  - dev (前50字符): $($json.dev.Substring(0, [Math]::Min(50, $json.dev.Length)))..." -ForegroundColor Gray
    Write-Host "  - last_box (前50字符): $($json.last_box.Substring(0, [Math]::Min(50, $json.last_box.Length)))..." -ForegroundColor Gray
    
} catch {
    Write-Host "❌ API调用失败: $_" -ForegroundColor Red
}

# 4. 等待并显示Frida输出
Write-Host "`n========================================" -ForegroundColor Green
Write-Host "📊 Frida Hook 输出:" -ForegroundColor Cyan
Write-Host "========================================`n" -ForegroundColor Green

Start-Sleep -Seconds 2

# 获取Frida输出
$output = Receive-Job -Job $fridaJob -Keep
if ($output) {
    Write-Host $output -ForegroundColor White
} else {
    Write-Host "⏳ 暂无输出，继续等待..." -ForegroundColor Yellow
    Start-Sleep -Seconds 3
    $output = Receive-Job -Job $fridaJob -Keep
    if ($output) {
        Write-Host $output -ForegroundColor White
    }
}

# 5. 询问是否再次调用
Write-Host "`n========================================" -ForegroundColor Green
Write-Host "💡 提示: Frida仍在运行" -ForegroundColor Cyan
Write-Host "========================================`n" -ForegroundColor Green

Write-Host "按 Enter 再次调用API，或输入 Q 退出..." -ForegroundColor Yellow
while ($true) {
    $input = Read-Host
    
    if ($input -eq "Q" -or $input -eq "q") {
        Write-Host "`n👋 停止Frida..." -ForegroundColor Yellow
        Stop-Job -Job $fridaJob
        Remove-Job -Job $fridaJob
        Write-Host "✅ 已退出`n" -ForegroundColor Green
        break
    }
    
    Write-Host "`n🚀 再次调用API..." -ForegroundColor Cyan
    try {
        $response = Invoke-WebRequest -Uri $apiUrl -TimeoutSec 10
        Write-Host "✅ API调用成功！状态码: $($response.StatusCode)`n" -ForegroundColor Green
        
        # 显示最新的Frida输出
        Start-Sleep -Seconds 1
        $newOutput = Receive-Job -Job $fridaJob -Keep
        if ($newOutput) {
            # 只显示新的输出（简单方法：显示最后50行）
            $lines = $newOutput -split "`n"
            $lastLines = $lines | Select-Object -Last 50
            Write-Host ($lastLines -join "`n") -ForegroundColor White
        }
        
    } catch {
        Write-Host "❌ API调用失败: $_`n" -ForegroundColor Red
    }
    
    Write-Host "`n按 Enter 再次调用API，或输入 Q 退出..." -ForegroundColor Yellow
}


